<template>
    <div class="app-container">
        <el-card>
            <!-- <el-button type="primary" @click="openTax"> 打开税务局</el-button> -->
            <div
                style="display: flex;justify-content: space-between; align-items: center; width: 100%; position: relative;">
                <el-tabs v-model="whereForm.servicePeriod" @tab-click="handleClick" style="width: 100%;">
                    <el-tab-pane v-for="item in client_service_periodList" :key="item.value"  :label="item.label" :name="item.value"></el-tab-pane>
                </el-tabs>
                <el-button @click="addCustomer" type="primary" size="small"
                    style="position: absolute;right: 0; top: 0%; width: 100px;" icon="el-icon-plus">新增</el-button>
            </div>
            <div>
                客户分类：
                <el-radio-group v-model="whereForm.clientProperty" @change="handleClick" size="medium">
                    <el-radio-button label="">全部</el-radio-button>
                    <el-radio-button v-for="item in client_propertyList" :key="item.value" :label="item.value">{{ item.label }}</el-radio-button>
                </el-radio-group>
            </div>
            <el-table style="margin-top: 20px;" :data="tableData.data" border>
                <el-table-column label="ID" width="80" prop="id" align="center">
                </el-table-column>
                <el-table-column label="客户名称" prop="clientName" align="center">
                </el-table-column>
                <el-table-column label="助记码" prop="helpCode" align="center">
                </el-table-column>
                <el-table-column label="纳税人识别号" prop="creditCode" align="center">
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editItem(scope.row)">编辑</el-button>
                        <el-button type="text" @click="deleteItem(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="pageRight" background @size-change="handleSizeChange"
                @current-change="handleCurrentChange" :page-sizes="[10, 20, 30, 40]" :current-page="whereForm.pageNum"
                :page-size="whereForm.pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="tableData.total">
            </el-pagination>
        </el-card>
        <basicinfo @getList="getList" ref="basicinfoRef"></basicinfo>
        <taxlog ref="taxlogRef"></taxlog>
    </div>
</template>

<script>
import { taxInfo } from './options'
import basicinfo from './components/basicinfo.vue';
import taxlog from './components/taxlog.vue';
import { getDictData } from '@/utils/dict'
import { getCreditCustomer, deleteCreditCustomerPerson } from '@/api/customer'
export default {
    components: { basicinfo, taxlog },
    data() {
        return {

            whereForm: {
                servicePeriod: '',
                clientProperty: '',
                pageNum: 1,
                pageSize: 10
            },
            tableData: {
                data: [],
                total: 0
            },
            listLoading: false,
            client_propertyList: [],
            client_service_periodList:[]
        }
    },
    created() {
        this.getDictData()
        this.getList()

    },
    methods: {
        getDictData() {
            let dictList = ['client_property', 'client_service_period']
            dictList.forEach(v => {
                getDictData(v).then(res => {
                    this[v + 'List'] = res
                })
            })
        },
        handleSizeChange(val) {
            this.whereForm.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.whereForm.pageNum = val;
            this.getList();
        },
        handleClick() { 
            this.whereForm.pageNum = 1;
            this.getList();
         },
        getList() {
            this.listLoading = true;
            getCreditCustomer(this.whereForm).then(res => {
                this.tableData.data = res.data.list
                this.tableData.total = res.data.total
            }).finally(() => {
                this.listLoading = false
            })
        },
        deleteItem(val) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCreditCustomerPerson(val.id).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    this.getList()
                })
            })
        },
        editItem(val) {
            this.$refs.basicinfoRef.open('edit', val)
        },
        addCustomer() {
            this.$refs.basicinfoRef.open('create')
        },
        openTax() {
            const script = document.createElement("script");
            script.src = "https://manage.shengdaiqifu.com/admin-web/#/"
            document.body.appendChild(script);
            // document.cookie =`name=zhangsan; path=/; domain=manage.shengdaiqifu.com`
            document.cookie = "name=zhangsan; Secure; HttpOnly; SameSite=Strict"
            window.open(script.src)

            // this.$refs.taxlogRef.open('')
            return
            const newWindow = window.open('', '_blank');
            if (!newWindow) {
                alert('请允许弹出窗口');
                return;
            }
            newWindow.location.href = taxInfo.url;
            // window.open(taxInfo.taxUrl)
            newWindow.onload = () => {
                try {
                    // 设置Cookies
                    Object.entries(taxInfo.Cookies).forEach(([name, value]) => {
                        if (value !== null) {
                            newWindow.document.cookie = `${name}=${value}; path=/; domain=.guangdong.chinatax.gov.cn`;
                        }
                    });

                    // 设置localStorage
                    Object.entries(taxInfo.localStorage).forEach(([key, value]) => {
                        newWindow.localStorage.setItem(key, value);
                    });

                    // 可选：重新加载使Cookies生效
                    newWindow.location.reload();
                } catch (error) {
                    console.error('跨域限制，无法直接操作存储:', error);
                    // 如果跨域，这里会报错，需要其他方法
                }
            };

            setTimeout(() => {
                // newWindow.location.href = taxInfo.linkUrl;
            }, 6000);
        }
    }
}



// https://tpass.guangdong.chinatax.gov.cn:8443/#/login?redirect_uri=https://etax.guangdong.chinatax.gov.cn:8443/mhzx/api/mh/tpass/code&client_id=c5djdncfa7nj4n2cajna2j68cndbj2fc&response_type=code&state=672dbd6617c443f9aa7d444f39215d4a
</script>

<style></style>
